package com.ytjj.qmyx.admin.service.function.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.admin.dao.YxFunctionConfigDAO;
import com.ytjj.qmyx.admin.dao.YxFunctionConfigNewuserDAO;
import com.ytjj.qmyx.admin.model.dto.YxFunctionConfigNewuserDto;
import com.ytjj.qmyx.admin.service.function.FunctionConfigUserService;
import com.ytjj.qmyx.mall.model.YxFunctionConfig;
import com.ytjj.qmyx.mall.model.YxFunctionConfigKefu;
import com.ytjj.qmyx.mall.model.YxFunctionConfigNewuser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FunctionConfigUserServiceImpl implements FunctionConfigUserService {

    @Resource
    private YxFunctionConfigDAO yxFunctionConfigDAO;

    @Resource
    private YxFunctionConfigNewuserDAO yxFunctionConfigNewuserDAO;

    @Override
    public YxFunctionConfigNewuserDto getDetailById(Integer id) {
        YxFunctionConfigNewuser vo = yxFunctionConfigNewuserDAO.selectByEventId(id);
        YxFunctionConfigNewuserDto dto = new YxFunctionConfigNewuserDto();
        BeanUtils.copyProperties(vo, dto);
        YxFunctionConfig config = yxFunctionConfigDAO.selectByPrimaryKey(id);
        dto.setName(config.getName());
        dto.setDesc(config.getDesc());
        return dto;
    }

    @Override
    public Map getDetail(Integer id) {
        YxFunctionConfig config = yxFunctionConfigDAO.selectByPrimaryKey(id);
        if(1==id){
            YxFunctionConfigNewuser vo = yxFunctionConfigNewuserDAO.selectByEventId(id);
            YxFunctionConfigNewuserDto dto = new YxFunctionConfigNewuserDto();
            BeanUtils.copyProperties(vo, dto);

            dto.setName(config.getName());
            dto.setDesc(config.getDesc());
            dto.setConfigId(config.getId());

            String jsonStr = JSONObject.toJSONString(dto);
            return JSONObject.parseObject(jsonStr);
            //return dto;
        }
        if(2==id){
            Map map = new HashMap<>();
            List<Map> list = yxFunctionConfigNewuserDAO.getFunctionConfigKefu();
            map.put("list",list);
            map.put("name",config.getName());
            map.put("desc",config.getDesc());
            map.put("configId",config.getId());
            return  map;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(YxFunctionConfigNewuserDto param) {
        YxFunctionConfig config = yxFunctionConfigDAO.selectByPrimaryKey(param.getConfigId());
        if(StrUtil.isNotEmpty(param.getDesc()) &&  !param.getDesc().equals(config.getDesc())){
            config.setDesc(param.getDesc());
            yxFunctionConfigDAO.updateByPrimaryKeySelective(config);
        }
        if(1==param.getConfigId()){
            YxFunctionConfigNewuser vo = new YxFunctionConfigNewuser();
            BeanUtils.copyProperties(param, vo);
            yxFunctionConfigNewuserDAO.updateByPrimaryKeySelective(vo);
        }
        if(2==param.getConfigId()){
            List<YxFunctionConfigKefu>  list = param.getList();
            for (YxFunctionConfigKefu vo:list){
                yxFunctionConfigNewuserDAO.updateByConfigKefu(vo);
            }
        }


    }
}
